package com.example.ecas.json_algorithm;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.json.JSONObject;
import org.python.antlr.ast.Str;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class test {
    public static void main(String[] args) throws JsonProcessingException {
        System.out.println("test");
        String filePath="\\practiseTraining\\ecasProject2\\workspace\\ecas\\src\\main\\resources\\upload\\triplet.txt";
        String data="";
        ArrayList<String> list=new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                list.add(line);

                // 在这里对每一行的内容进行处理
            }
        } catch (IOException e) {
            e.printStackTrace();
            // 处理异常情况
        }


        //处理数据导出未语义对齐的json文件

        String []s1=new String[list.size()];
        int index=0;
        for(String str:list){
            s1[index]=str.substring(1,str.length()-1);
            index++;
        }

        //s2[i]装第i行2个实体和1个关系
        int lenx=s1.length;
        int leny=3;
        String [][]s2=new String[lenx][leny];
        for(int i=0;i<lenx;i++){
            s2[i]=s1[i].split(",");
        }

        ArrayList<String>entity=new ArrayList<>();
        String content="";

        for(int i=0;i<lenx;i++){
            int size=entity.size();

            String o1=s2[i][0];
            String o2=s2[i][2];
            String r=s2[i][1];
            int identity1=-1;
            int identity2=-1;
            int identityR=-1;
//-----------------------------------------------确定id
            for(int m=0;m<size;m++){
                if(entity.get(m).equals(o1)){
                    identity1=m+1;
                    break;
                }
            }
            if(identity1==-1){
                entity.add(o1);
                size++;
                identity1=size;
            }


            for(int m=0;m<size;m++){
                if(entity.get(m).equals(o2)){
                    identity2=m+1;
                    break;
                }
            }
            if(identity2==-1){
                entity.add(o2);
                size++;
                identity2=size;
            }


            for(int m=0;m<size;m++){
                if(entity.get(m).equals(r)){
                    identityR=m+1;
                    break;
                }
            }
            if(identityR==-1){
                entity.add(r);
                size++;
                identityR=size;
            }

 //----------------------------------------------------

            Start start=new Start(identity1,new ArrayList<String>(),new Properties(o1),identity1);
            End end=new End(identity2,new ArrayList<String>(),new Properties(o2),identity2);
            Relationship relationship=new Relationship(identityR,identity1,identity2,r,new Properties(),identityR,identity1,identity2);

            ArrayList<Object> segments=new ArrayList<>();
            Segment segment=new Segment(start,relationship,end);
            segments.add(segment);
            P p=new P(start,end,segments,1);
            Obj obj=new Obj(p);


            JSONObject object = new JSONObject(obj);
            String json = object.toString();


            // Creating Object of ObjectMapper define in Jackson API
            ObjectMapper Obj = new ObjectMapper();

            // Converting the Java object into a JSON string
            String jsonStr = Obj.writeValueAsString(obj);
            // Displaying Java object into a JSON string

            content+=jsonStr;
            if (i != lenx - 1) {
                content += ",";
            }


        }

        System.out.println(content);

        FileWriter writer;
        try {
            writer = new FileWriter("\\practiseTraining\\ecasProject2\\workspace\\ecas\\src\\main\\resources\\upload\\json11.json"); // 如果已存在，以覆盖的方式写文件
            // writer = new FileWriter("testFileWriter.txt", true); // 如果已存在，以追加的方式写文件
            writer.write(""); //清空原文件内容
            writer.write("["+content+"]");
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }




    }
}
